package com.xuyang.fragment;


import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.bookmanager.BrowseActivity;
import com.example.bookmanager.R;
import com.xuyang.other.CategoryBean;

public class ListFragment extends Fragment {
	private RecyclerView mRecyclerView;
	private LayoutManager mLayoutManager;
	private MyRecyclerAdapter mAdapter;
	private ArrayList<CategoryBean> mList=new ArrayList<CategoryBean>();
	//类别
	String[] categorys={"马列主义\n毛泽东思想","哲学 ","社会科学总论","政治、法律","军事","经济","文化、科学\n教育、体育","语言、文字","文学",
			"艺术","历史、地理","自然科学总论","数理科学和化学","天文学、地理科学","生物科学","医学、卫生","农业科学","工业技术","交通运输","航空、航天","综合性图书","其它"};
	//图书类别
	String[] bookCategorys={"马列主义、毛泽东思想","哲学","社会科学总论","政治、法律","军事","经济","文化、科学、教育、体育","语言、文字","文学",
			"艺术","历史、地理","自然科学总论","数理科学和化学","天文学、地理科学","生物科学","医学、卫生","农业科学","工业技术","交通运输","航空、航天","综合性图书","其它"};
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		for (int i = 0; i < 22; i++) {
			CategoryBean bean=new CategoryBean(getResources().getDrawable(R.drawable.selector_item_category_back), categorys[i]);
			mList.add(bean);
		}
		View view = LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_list, container, false);
		mRecyclerView=(RecyclerView) view.findViewById(R.id.recycler_category);
		mLayoutManager=new GridLayoutManager(container.getContext(), 2);
		mRecyclerView.setLayoutManager(mLayoutManager);
		mRecyclerView.setHasFixedSize(true);
		mAdapter=new MyRecyclerAdapter();
		mRecyclerView.setAdapter(mAdapter);
		return view;
	}
	class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
		@Override
		public int getItemCount() {
			return mList.size();
		}
		@SuppressLint("NewApi") @Override
		public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
//			viewHolder.layoutCategory.setBackground(mList.get(position).getDrawable());
			viewHolder.txtCategory.setText(mList.get(position).getCategory());
			viewHolder.layoutCategory.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
				    	Intent intent=new Intent(getActivity().getApplicationContext(),BrowseActivity.class);
					    intent.putExtra("bookCategory",bookCategorys[position]);
					    startActivity(intent);
				}
			});
		}
		@Override
		public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
			View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_category, viewGroup,false);
			ViewHolder viewHolder=new ViewHolder(view);
			return viewHolder;
		}
		public class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder{
			TextView txtCategory;
			LinearLayout layoutCategory;
			public ViewHolder(View view) {
				super(view);
				txtCategory=(TextView) view.findViewById(R.id.txt_category_item);
				layoutCategory=(LinearLayout) view.findViewById(R.id.layout_category_item);
			}
			
		}
	}
}
